#include "space.h"
#include "director.h"

OPEN_JLIB_NS


SpaceNode::SpaceNode (RootNode* root) noexcept : Node(root) {
  _inlineFlags |= StepEnabled;
}


void SpaceNode::dispatchDraw () {
  for (auto camera : cameras()) {
    if (!camera->isDrawEnabled())
      continue;
    _currentCamera = camera;
    camera->initDraw();
    Mat4 P = camera->getProjectionMatrix();
    Mat4 V = camera->getViewMatrix();
    Mat4 M = getLocalMatrix();
    Node::dispatchDraw(P * V, P, V, M);
    camera->endDraw();
    _currentCamera = nullptr;
  }
}

void SpaceNode::addCamera (CameraNode* camera, s64 stepOrder, s64 drawOrder) noexcept {
  addChild(camera, stepOrder, drawOrder);
  _cameras.push_back(&camera->_cameraList);
}


void SpaceNode::dispatchDraw (const Mat4& pv, const Mat4& p, const Mat4& v, const Mat4& m) {
  if (_cameras.only())
    Node::dispatchDraw(pv, p, v, m);
  else
    dispatchDraw();
}


const Mat4& SpaceNode::getSpaceMatrix () {
  return getLocalMatrix();
}



CLOSE_JLIB_NS
